<?php
ob_start();

$orderId = $_POST['pdforderid'];
//echo 'iiiiiiiiii'.$orderId;

$host="localhost"; // Host name 
$username="hambaaco_app"; // Mysql username  
$password="sdfsiJdsf435jf"; // Mysql password 
$db_name="hambaaco_tshirts"; // Database name 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// To protect MySQL injection (more detail about MySQL injection)
$orderId = stripslashes($orderId);
$orderId = mysql_real_escape_string($orderId);

$orders_sql="select id_order,nm_fname,nm_lname,tx_addr1,tx_addr2,tx_city,tx_state,cd_pincode,tx_email,tx_contact_no,DATE_FORMAT(dt_order,'%d %b %Y, %h:%i %p ') as dt_order,tx_shipping_addr,st_order from orders where cd_order=".$orderId;
$result_orders_sql=mysql_query($orders_sql);
$row_orders= mysql_fetch_array($result_orders_sql);

$order_details_sql="select nm_product,id_order,order_details.id_product,quantity,order_details.price from products, order_details where products.id_product =order_details.id_product and id_order=".$row_orders['id_order'];
$result_order_details_sql=mysql_query($order_details_sql);

$tableproductdetails=
		'<table border ="0" name="tableproductdetails" cellpadding="3">
		<tr align="center">
			<td><h4>Product</h4></td>
			<td><h4>Quantity</h4></td>
			<td><h4>Unit Price</h4></td>
			<td><h4>Sub Total</h4></td>
		</tr>';
$sub_total=0;
while($row_order_details = mysql_fetch_array($result_order_details_sql))
{
	$tx_id_order=$row_order_details['id_order'];
	$tx_id_product=$row_order_details['id_product'];
	$tx_nm_product=$row_order_details['nm_product'];
	$tx_quantity=$row_order_details['quantity'];
	$tx_price=$row_order_details['price'];
	$tx_sub_total=$row_order_details['quantity']*$row_order_details['price'];
	$sub_total+=$tx_sub_total;

$tableproductdetails.=
		'<tr align="center">
			<td>'.$tx_nm_product.'</td>
			<td>'.$tx_quantity.'</td>
			<td>'.$tx_price.'</td>
			<td>'.$tx_sub_total.'</td>
		</tr>';
}
$tableproductdetails.=
		'<tr align="center"  bgcolor="#DDD">
			<td><h4></h4></td>
			<td><h4></h4></td>
			<td><h4>Grand Total</h4></td>
			<td><h4>'.$sub_total.'</h4></td>
		</tr>';
$tableproductdetails.='</table>';



require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');



// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('www.hambaa.com');
$pdf->SetTitle('Order Summary Document');
$pdf->SetSubject('Hambaa Order Confirmation');
//$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING, array(80,80,80), array(80,80,80));
$pdf->setFooterData($tc=array(0,64,0), $lc=array(0,64,128));

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

//set some language-dependent strings
$pdf->setLanguageArray($l);

// ---------------------------------------------------------

// set default font subsetting mode
$pdf->setFontSubsetting(true);

// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('times', '', 14, '', true);

// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();


$tx_shipping_addr = $row_orders['nm_fname'].' '.$row_orders['nm_lname'].'<br>'.$row_orders['tx_addr1'].'<br>Landmark : '.$row_orders['tx_addr2'].'<br>'.$row_orders['tx_city'].'<br>'.$row_orders['tx_state'].' '.$row_orders['cd_pincode'].'<br>Contact No. : '.$row_orders['tx_contact_no'].'<br>e-mail : '.$row_orders['tx_email'];

$tx_order_summary = $orderId.'<br>'.$row_orders['dt_order'].'<br>'.$row_orders['st_order'];


$html = <<<EOD


<table border ="0" cellspacing="10" width="100%">
<tr><td colspan="3">
<h4>Shipping Address</h4>
</td></tr>
<tr>
<td width="53%">
$tx_shipping_addr
</td>
<td align="right" width="17%">
Order No.: <br>
Date &amp; Time: <br>
Status : <br>
</td>
<td width="30%">
$tx_order_summary
</td>
</tr>
<tr><td colspan="3">
<b>Payment Mode : </b>Cash On Delivery
</td></tr>
<tr><td colspan="3">
$tableproductdetails	
</td></tr>
<tr><td colspan="3" align="center">
<br><br>
<hr>
</td></tr>
<tr><td colspan="3" align="center">
<h4>Thank you for chosing hambaa. Have a nice day!</h4>
</td></tr>

</table>

EOD;
//$html = $html. $row['nm_fname'];
// Print text using writeHTMLCell()
$pdf->writeHTMLCell($w=0, $h=0, $x='', $y='', $html, $border=0, $ln=1, $fill=0, $reseth=true, $align='', $autopadding=true);

// ---------------------------------------------------------

// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('example_001.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+
ob_end_flush();

?>
